import 'package:flutter/material.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/my_controller/gift_controller/gift_order_page_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/utils/refresh_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class GiftOrderPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: pageBgColor,
      appBar: AppBarUtil(
        title: '赠品订单',
        backgroundColor: pageBgColor,
      ),
      body: GetBuilder<GiftOrderPageController>(
        init: GiftOrderPageController(),
        builder: (controller) {
          if (controller.giftOrderListVo.value.data!.length <= 0) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image.asset(
                    'assets/icons/gift_list_def.png',
                    width: 273.w,
                    height: 146.h,
                  ),
                  Text(
                    '暂无赠品哦~',
                    style: TextStyle(
                      color: textSubtitleColor,
                      fontSize: 14.sp
                    ),
                  )
                ],
              ),
            );
          }
          return ScrollConfigurationCommon(child: RefreshUtil.easyRefresh(
            onRefresh: () async {
              controller.getGitfOrderList();
            },
            onLoad: () async {
              controller.onloadGitfOrderList();
            },
            slivers: controller.giftOrderListVo.value.data!.map((item) {
              return SliverToBoxAdapter(
                child: InkWellUtil.shakeProofInkWell(Container(
                  margin: EdgeInsets.only(top: 12.h),
                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 8.w),
                    decoration: BoxDecoration(
                      color: bgWhiteColor,
                      borderRadius: BorderRadius.circular(5.w)
                    ),
                    child: Column(
                      children: [
                        Container(
                          padding: EdgeInsets.symmetric(horizontal: 4.w),
                          height: 44.h,
                          decoration: BoxDecoration(
                            border: Border(
                              bottom: BorderSide(
                                color: lineColor,
                                width: .5
                              )
                            )
                          ),
                          child: Row(
                            children: [
                              Expanded(child: Text(
                                '订单号: ${item.orderSn}',
                                overflow: TextOverflow.ellipsis,
                                maxLines: 1,
                                style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  fontSize: 14.sp,
                                  color: textBlackColor
                                ),
                              )),
                              Text(
                                '${item.statusStr}',
                                style: TextStyle(
                                  color: mainTextColor,
                                  fontSize: 14.sp
                                ),
                              )
                            ],
                          ),
                        ),
                        Container(
                          padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: item.products!.map((itemProducts) {
                              return Container(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      children: [
                                        NetworkImageUtil(
                                          url: itemProducts.image ?? '', 
                                          width: 76.w, 
                                          height: 76.w,
                                          borderRadius: 5.w,
                                        ),
                                        SizedBox(width: 12.w),
                                        Expanded(child: Container(
                                          constraints: BoxConstraints(
                                            minHeight: 76.w
                                          ),
                                          child: Column(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            mainAxisAlignment: MainAxisAlignment.start,
                                            children: [
                                              Row(
                                                crossAxisAlignment: CrossAxisAlignment.center,
                                                children: [
                                                  Expanded(child: Text(
                                                    '${itemProducts.title}',
                                                    overflow: TextOverflow.ellipsis,
                                                    maxLines: 2,
                                                    style: TextStyle(
                                                      color: textBlackColor,
                                                      fontSize: 14.sp
                                                    ),
                                                  )),
                                                  SizedBox(width: 6.w),
                                                  Text(
                                                    'x${itemProducts.quantity}',
                                                    style: TextStyle(
                                                      color: textSubtitleColor,
                                                      fontSize: 12.sp
                                                    ),
                                                  )
                                                ],
                                              ),
                                              SizedBox(height: 6.h),
                                              Text(
                                                '${item.createdAt}',
                                                style: TextStyle(
                                                  color: textSubtitleColor,
                                                  fontSize: 12.sp
                                                ),
                                              )
                                            ],
                                          ),
                                        )),
                                      ],
                                    ),
                                    SizedBox(height: 12.w),
                                    Container(
                                      alignment: Alignment.centerRight,
                                      child: Text.rich(
                                        TextSpan(
                                          text: '合计支付: ',
                                          style: TextStyle(
                                            color: textBlackColor,
                                            fontSize: 14.sp,
                                            fontWeight: FontWeight.bold
                                          ),
                                          children: [
                                            TextSpan(
                                              text: '¥',
                                              style: TextStyle(
                                                color: textBlackColor,
                                                fontSize: 12.sp,
                                                fontWeight: FontWeight.bold
                                              ),
                                            ),
                                            TextSpan(
                                              text: '${item.totalAmount}',
                                              style: TextStyle(
                                                color: textBlackColor,
                                                fontSize: 16.sp,
                                                fontWeight: FontWeight.bold
                                              ),
                                            )
                                          ]
                                        )
                                      ),
                                    )
                                  ],
                                ),
                              );
                            }).toList(),
                          ),
                        )
                      ],
                    ),
                  ),
                ), () {
                  Get.toNamed(
                    '${AppRouters.COMMON_PAGE}${AppRouters.GIFT_ORDER_PAGE}${AppRouters.GIFT_ORDER_DETAIL_PAGE}',
                    arguments: {
                      'orderId': item.id
                    }
                  );
                }),
              );
            }).toList()
          ));
        },
      ),
    );
  }
}

// class GiftOrderPage extends StatelessWidget {

//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       backgroundColor: pageBgColor,
//       appBar: AppBarUtil(
//         title: '赠品订单',
//         backgroundColor: pageBgColor,
//       ),
//       body: GetBuilder(

//       )
//     );
//   }
// }